
// num8 => Date Object
export function eightNum2Date(timeString) {
  timeString = "" + timeString;
  
  //const timeString = '20220322123456'; // 2022年3月22日12点34分56秒
  const year = parseInt(timeString.substr(0, 4));
  const month = parseInt(timeString.substr(4, 2)) - 1; // 月份从0开始计数，所以要减去1
  const day = parseInt(timeString.substr(6, 2));
  // const hour = parseInt(timeString.substr(8, 2));
  // const minute = parseInt(timeString.substr(10, 2));
  // const second = parseInt(timeString.substr(12, 2));

  const date = new Date(year, month, day, 0, 0, 0);
  return date;
}


export function _dateTimeFormatter2Number(date) {
  const year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  let hour = date.getHours();
  let minute = date.getMinutes();
  let second = date.getSeconds();

  month = `0${month}`.slice(-2);
  day = `0${day}`.slice(-2);
  hour = `0${hour}`.slice(-2);
  minute = `0${minute}`.slice(-2);
  return `${year}${month}${day}`;
}

export function _dateTimeFormatter(date) {
  const year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  let hour = date.getHours();
  let minute = date.getMinutes();
  let second = date.getSeconds();

  month = `0${month}`.slice(-2);
  day = `0${day}`.slice(-2);
  hour = `0${hour}`.slice(-2);
  minute = `0${minute}`.slice(-2);

  // return `${day}.${month}.${year} ${hour}:${minute}`; // by srx
  return `${year}/${month}/${day} ${hour}:${minute}`
}

// 时间戳转日期格式
export function formatDate(now) {
  var year = now.getFullYear();
  var month = now.getMonth() + 1;
  var date = now.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  };
  if (date >= 0 && date <= 9) {
    date = "0" + date;
  };
  return year + "-" + month + "-" + date;
}
// 日期格式转时间戳
export function dateToMs(date) {
  return new Date(date).getTime()
}

// 获取输入日期的前三月
export function defaultThreeMonths(localdate) {// localdate is String . "2023-03-02"
  var newdate = new Date(localdate.replace(/-/g, "/"))
  newdate = newdate.setMonth(newdate.getMonth() - 3)
  var aa = new Date(newdate);
  return formatDate(aa); // 时间戳转日期格式
}

// 获取输入时间的上一年
export function getLastYear(localdate) { // localdate is String . "2023-03-02"
  var newdate = new Date(localdate.replace(/-/g, "/"))
  newdate = newdate.setFullYear(newdate.getFullYear() - 1)
  var aa = new Date(newdate);
  return formatDate(aa); // 时间戳转日期格式
}

export function getTodayLastYear() {
  var newdate = new Date();
  newdate = newdate.setFullYear(newdate.getFullYear() - 1)
  var aa = new Date(newdate);
  return aa;
  //return formatDate(aa); // 时间戳转日期格式
}

// 今天的下一年
export function getFutureOneMoreYear() {
  var newdate = new Date();
  newdate = newdate.setFullYear(newdate.getFullYear() + 1)
  var aa = new Date(newdate);
  return aa;
  //return formatDate(aa); // 时间戳转日期格式
}
